package com.taysear.parcel.fieldfactory;

import com.taysear.parcel.ParcelApplication;
import com.taysear.parcel.domain.AccountStatus;
import com.taysear.parcel.domain.Customer;
import com.taysear.parcel.domain.Station;
import com.taysear.parcel.domain.StationStatus;
import com.taysear.parcel.util.Constants;
import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.addon.jpacontainer.JPAContainerFactory;
import com.vaadin.addon.jpacontainer.fieldfactory.FieldFactory;
import com.vaadin.data.Item;
import com.vaadin.data.validator.DoubleValidator;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.Field;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.TextField;

public class AccountFieldFactory extends FieldFactory {
	
	ParcelApplication app;
	public final TextField id = new TextField("Account id");
	public NativeSelect customer = new NativeSelect("Customer");
	final TextField openingBalance = new TextField("Opening balance");
	final TextField name = new TextField("Name");
	NativeSelect accountStatus = new NativeSelect("Status");

	public AccountFieldFactory(ParcelApplication app) {
		this.app = app;
	}

	@Override
	public Field createField(Item item, Object propertyId, Component uiContext) {
		Field f = null;
		if ("id".equals(propertyId)) {
			id.setRequired(true);
			id.setWidth(Constants.COMMON_FIELD_WIDTH);
			return id;
		} if ("openingBalance".equals(propertyId)) {
			openingBalance.setWidth(Constants.COMMON_FIELD_WIDTH);
			openingBalance.setNullRepresentation("");
			openingBalance.addValidator(new DoubleValidator("Enter correct amount"));
			return openingBalance;
		} if ("name".equals(propertyId)) {
			name.setRequired(true);
			name.setWidth(Constants.COMMON_FIELD_WIDTH);
			name.setRequiredError("Name is required");
			name.setNullRepresentation("");
			return name;
		} if ("customer".equals(propertyId)) {
			try{
				customer = (NativeSelect) super.createField(item, propertyId, uiContext);
			} catch (ClassCastException e) {
				JPAContainer<Customer> container = JPAContainerFactory.make(Customer.class, app.PERSISTENCE_UNIT);
				customer.setContainerDataSource(container);
			}
			customer.setWidth(Constants.COMMON_FIELD_WIDTH);
			customer.setNullSelectionAllowed(false);
			customer.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
			customer.setRequired(true);
			customer.setRequiredError("Customer is required");
			return customer;
		} if ("accountStatus".equals(propertyId)) {
			try{
				accountStatus = (NativeSelect) super.createField(item, propertyId, uiContext);
			} catch (ClassCastException e) {
				JPAContainer<AccountStatus> container = JPAContainerFactory.make(AccountStatus.class, app.PERSISTENCE_UNIT);
				accountStatus.setContainerDataSource(container);
			}
			accountStatus.setWidth(Constants.COMMON_FIELD_WIDTH);
			accountStatus.setNullSelectionAllowed(false);
			accountStatus.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
			accountStatus.setRequired(true);
			accountStatus.setRequiredError("Status is required");
			return accountStatus;
		} else {
			// Use the super class to create a suitable field base on the
			// property type.
			f = super.createField(item, propertyId, uiContext);
		}
		return f;
	}
}
